findEventDetail();
findMarketCountDetail();

var data_source;
var page_count = 15;

/**
 * 查询比赛详情
 */
function findEventDetail() {

    $.ajax({
        url: baseUrl.base + '/country/eventInfo',
        type: 'get',
        data: {"eventId": baseFn.getQueryString('eventId')},
        success: function (data) {

            if (data.code == '0') {
                //创建成功

                $('#home_name_1').text(data.results.homeTeamName);
                $('#home_name_2').text(data.results.homeTeamName);

                $('#away_name_1').text(data.results.awayTeamName);
                $('#away_name_2').text(data.results.awayTeamName);

                var competitionName = data.results.competitionName;
                var teamName = data.results.homeTeamName + 'VS' + data.results.awayTeamName;
                var eventTime = data.results.eventTime / 10000;
                var score = data.results.homeTeamScore + ' : ' + data.results.awayTeamScore;
                var state = data.results.state;
                var stage = data.results.stage;

                var category = '';
                category += competitionName + "  ";
                category += teamName + ' ';

                var str = '';
                if (state == 3) {
                    if (stage == 2) {
                        str = "比赛正在上半场";
                    } else if (stage == 4) {
                        str = "比赛正在中场休息";
                    } else if (stage == 8) {
                        str = "比赛正在下半场";
                    } else if (stage == 16) {
                        str = "比赛全场结束";
                    } else if (stage == 32) {
                        str = "比赛常规时间结束";
                    } else if (stage == 64) {
                        str = "比赛正在加时赛上半场";
                    } else if (stage == 128) {
                        str = "比赛正在加时赛中场休息";
                    } else if (stage == 256) {
                        str = "比赛正在加时赛下半场";
                    } else if (stage == 512) {
                        str = "比赛加时赛结束";
                    } else if (stage == 1024) {
                        str = "比赛正在点球大战";
                    }
                }
                category += str;

                if (state != 0) {
                    category += '比分' + score;
                }

                $('#card-category').text(category);

                $('#card-title').text('开赛时间:' + timestampToTime(eventTime));
            }
        }
    })
}

/**
 * 查询统计明细
 */
function findMarketCountDetail() {
    var eventId = baseFn.getQueryString('eventId');
    var ruleType = baseFn.getQueryString('ruleType');
    var selectionId = baseFn.getQueryString('selectionId');

    $.ajax({
        url: baseUrl.base + '/order/selectOrderCountDetail',
        type: 'get',
        data: {"eventId":eventId,"ruleType":ruleType,"selectionId":selectionId},
        success: function (data) {

            if (data.code == '0') {
                var res = data.results;

                data_source = res;

                cutPagination(1);

                // 配置分页
                $("#p1").pagination({
                    totalData: res.length,
                    showData:page_count,
                    callback: function (idx) {
                        cutPagination(idx);
                    }
                });
            }
        }
    })
}

function statusStr(status) {
    var str = '未知';
    if (status == 0){
        str = '等待确认';
    }else if(status == 1){
        str = '等待结算';
    }else if(status == 2){
        str = '已结算';
    }else if(status == 3){
        str = '被取消';
    }else if(status == 4){
        str = '无效';
    }else if(status == 5){
        str = '被拒绝';
    }
    return str;
}


/**
 * 分页
 * @param page 从1开始
 */
function cutPagination(page) {

    var start = (page - 1) * page_count;
    var end = start + page_count;

    if (end > data_source.length){
        end = data_source.length;
    }

    $('#tbody').empty();

    for (var i = start;i < end;i ++){
        var count = data_source[i];

        var user_phone = count.user_phone;
        var order_money = count.order_money;
        var fictitious_money = count.fictitious_money;
        var create_time = count.create_time;
        var order_odds = count.order_odds;
        var order_state = count.order_state;
        var isWin = count.isWin;

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var tdContent = document.createTextNode(i + 1 + '');
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(user_phone);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var str = order_money + '';
        if (fictitious_money > 0){
            str += '(' + fictitious_money + ')';
        }
        str += ' KSH';

        var tdContent = document.createTextNode(str);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(timestampToTime(create_time));
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(order_odds);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var str = statusStr(order_state);
        if (status == 2) {
            if (isWin == 1) {
                str += " · 赢";
            } else {
                str += " · 输";
            }
        }
        var tdContent = document.createTextNode(str);
        td.appendChild(tdContent);
        tr.appendChild(td);

        document.getElementById("tbody").append(tr);
        document.getElementById("tbody").innerHTML;
    }

}